<template>
  <div>
    <h1>我是didi</h1>
    {{ count }}
    <button @click="byebye">拜拜</button>
  </div>
</template>

<script>
import PubSub from "pubsub-js";
export default {
  name: "Didi",
  data() {
    return {
      //一般我们再接收发布信息之前，都要书写一个初始值
      count: 0,
      pubId: null,
    };
  },
  //先订阅，一般再初始化环节就订阅 mounted
  mounted() {
    this.pubId = PubSub.subscribe("count", (_, count) => {
      console.log(count);
      this.count = count;
    });
  },
  methods: {
    byebye() {
      //unsubscribe也可接受一个订阅id(pubId),是订阅方法的返回值,可以取消当前组件的当前订阅
      PubSub.unsubscribe(this.pubId);
    },
  },
};
</script>

<style>
</style>